package br.com.passeionaweb.appengine.tabelafipe.data;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable
public class Make {
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long id;

	public static final String KEY_MAKE = "make";

	@Persistent
	private String make;

	public String getMake() {
		return make;
	}

	public void setMake(String make) {
		this.make = make;
	}

	public Long getId() {
		return id;
	}

	public Make(String make) {
		super();
		this.make = make;
	}

	static public boolean exists(String make) {
		return getMake(make) != null;
	}

	static public Make getMake(String make) {
		try {
			PersistenceManager pm = PMF.get().getPersistenceManager();
			Query query = pm.newQuery(Make.class);
			query.setFilter("make == param_make");
			query.declareParameters("String param_make");
			List<Make> makes = (List<Make>) query.execute(make);
			if (makes.isEmpty()) {
				return null;
			} else {
				return makes.get(0);
			}
		} catch (Exception e) {
			return null;
		}
	}

	static public Make getMake(long makeid) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		return pm.getObjectById(Make.class, makeid);
	}
}
